package top.fengyulou.code.controller.front.api;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.fengyulou.code.entity.Api;
import top.fengyulou.code.entity.ApiForm;
import top.fengyulou.code.service.ApiService;
import top.fengyulou.code.service.ProjectService;
import top.fengyulou.common.Jr;
import top.fengyulou.common.UserUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 接口管理
 */
@Controller
@RequestMapping("/f/api")
public class FrontApiController {

    @Resource
    private ApiService apiService;
    @Resource
    private ProjectService projectService;

    /**
     * 列表页面
     *
     * @return
     */
    @GetMapping("/")
    public String list(HttpServletRequest request, ApiForm form) {
        //查询接口
        form.setProjectId(projectService.getSessionProjectId(request.getSession()));
        List<ApiForm> list = apiService.getList(form);
        request.setAttribute("list", list);
        return "api-list";
    }

    /**
     * 添加操作
     */
    @PostMapping("/ajax/add")
    @ResponseBody
    public Jr ajaxAdd(HttpServletRequest request, ApiForm form) {
        //检查数据
        Jr jr = checkForm(form);
        if (!jr.isStatus()) {
            return jr;
        }
        form.setProjectId(projectService.getSessionProjectId(request.getSession()));
        //添加操作
        long apiCount = apiService.getCountByUrlAndProjectId(form);
        if (apiCount == 0) {
            form.setApiStatus(Api.API_STATUS_YESNO);
            form.setProjectId(projectService.getSessionProjectId(request.getSession()));
            form.setUserId(UserUtils.getUserId(request));
            form.setAuthor(UserUtils.getLoginName(request));
            apiService.add(form);
        } else {
            return Jr.no("接口已存在");
        }
        return Jr.yes();
    }

    /**
     * 修改操作
     */
    @PostMapping("/ajax/update")
    @ResponseBody
    public Jr ajaxUpdate(HttpServletRequest request, ApiForm form) {
        //检查数据
        Jr jr = checkForm(form);
        if (!jr.isStatus()) {
            return jr;
        }
        form.setProjectId(projectService.getSessionProjectId(request.getSession()));
        //修改操作
        long apiCount = apiService.getCountByUrlAndProjectId(form);
        if (apiCount == 0) {
            form.setUserId(UserUtils.getUserId(request));
            apiService.update(form);
        } else {
            return Jr.no("接口已存在");
        }
        return Jr.yes();
    }

    /**
     * 删除操作
     */
    @PostMapping("/ajax/delete")
    @ResponseBody
    public Jr ajaxDelete(HttpServletRequest request, @RequestParam Long id) {
        apiService.delete(id, UserUtils.getUserId(request));
        return Jr.yes();
    }

    /**
     * 检查数据
     */
    private Jr checkForm(ApiForm form) {
        if (StringUtils.isBlank(form.getName())) {
            return Jr.no("未输入名称");
        }
        return Jr.yes();
    }

    /**
     * 完成
     *
     * @return
     */
    @PostMapping("/ajax/yesok")
    @ResponseBody
    public Jr ajaxYesok(@RequestParam Long id) {
        ApiForm v2ApiForm = new ApiForm();
        v2ApiForm.setId(id);
        v2ApiForm.setApiStatus(Api.API_STATUS_YESOK);
        apiService.updateApiStatusById(v2ApiForm);
        return Jr.yes();
    }

    /**
     * 取消完成
     *
     * @return
     */
    @PostMapping("/ajax/yesno")
    @ResponseBody
    public Jr ajaxYesno(@RequestParam Long id) {
        ApiForm v2ApiForm = new ApiForm();
        v2ApiForm.setId(id);
        v2ApiForm.setApiStatus(Api.API_STATUS_YESNO);
        apiService.updateApiStatusById(v2ApiForm);
        return Jr.yes();
    }

}
